// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// y [author] rhyme_lph
// a [email]  rhymelph@gmail.com
// i [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/bank_detail_record_item_entity.dart';
import 'package:finance_app/src/entities/bank_reconciliation_entity.dart';
import 'package:finance_app/src/http_service/http_bank_reconciliations_service.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/views/reconcile_account_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/dashboard_add_bottom_sheet.dart';
import 'package:flutter/material.dart';

import 'bank_reconciliation_provider.dart';

class ReconcileAccountDetailProvider
    extends BaseProvider<List<BankDetailRecordItemEntity>> {
  int id;

  List<BankDetailRecordItemEntity> selectReconcileList = [];

  String realDifference;

  String title;

  BankReconciliationEntity bank;
  String closingDate;

  num get difference {
    num difference = num.parse(realDifference);
    num selectTotal = 0;
    selectReconcileList.forEach((element) {
      if (element.flowType == 'in') {
        selectTotal += element.total;
      } else {
        selectTotal -= element.total;
      }
    });
    return difference - selectTotal;
  }

  void onSelectAllReconcile() {
    selectReconcileList.addAll(data);
    update();
  }

  void onDeselectAllReconcile() {
    selectReconcileList.clear();
    update();
  }

  void onAddNewReconcile() {
    obtainContext?.call((context) {
      showDashboardAddBottomSheet(context,
          dashTypes: [DashBoardAddType.accountTransaction]);
    });
  }

  @override
  Future get dataFuture => HttpBankReconciliationsService.paymentRecord(id, id);

  void onRequestRefresh() {
    post(dataFuture,
        autoHandler: AutoHandler.Toast,
        loadingTip: S.current.loading, onSuccess: (result) {
      data = getEntityFromResult<List<BankDetailRecordItemEntity>>(result);
      update();
    });
  }

  void onSelectReconcile(BankDetailRecordItemEntity value) {
    final finder = selectReconcileList
        .firstWhere((element) => element.id == value.id, orElse: () => null);
    if (finder == null) {
      selectReconcileList.add(value);
    } else {
      selectReconcileList.removeWhere((element) => element.id == value.id);
    }
    update();
  }

  void onReconcile() async {
    await Routes.navigateTo(RouterGenProviders.reconcileAccountConfirmPage,
        params: {
          'title': title,
          'selectReconcileList': selectReconcileList,
          'reconcileId': id,
          'closingDate': closingDate,
          'beginningBalance': bank.beginningBalance,
          'statementBalance': bank.statementBalance,
        });
    if (Providers.get<BankReconciliationProvider>(
                RouterGenProviders.bankReconciliationPage, null)
            ?.getChildProvider<ReconcileAccountProvider>()
            ?.isDestroy ==
        true) {
      obtainContext?.call((context) {
        Navigator.of(context).pop();
      });
    }
  }
}
